package net.caixiaomi.info.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import net.caixiaomi.info.R;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.BaseViewHolder;
import net.caixiaomi.info.model.CouponsItem;

import java.util.List;

/**
 * 优惠券
 */

public class CouponsAdapter extends BaseQuickAdapter<CouponsItem, BaseViewHolder> {

    public CouponsAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    public CouponsAdapter(@LayoutRes int layoutResId, @Nullable List<CouponsItem> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CouponsItem item) {
        try {
            int priceColor = 0;
            int nameColor = 0;
            int drawable = 0;
            helper.setGone(R.id.image, true);
            TextView btnNext = helper.getView(R.id.btn_next);
            btnNext.setVisibility(View.GONE);
            if (TextUtils.equals("0", item.getBonusStatus())) {
                priceColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary);
                nameColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text);
                btnNext.setEnabled(true);
                btnNext.setVisibility(View.VISIBLE);
                helper.addOnClickListener(R.id.btn_next);
                btnNext.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.orange_primary));
                if (TextUtils.equals("1", item.getSoonExprireBz())) {
                    drawable = R.mipmap.ic_exprie_soon;
                    helper.setGone(R.id.image, true);
                } else if (TextUtils.equals("2", item.getSoonExprireBz())) {
                    btnNext.setEnabled(false);
                    btnNext.setTextColor(ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text));
                    drawable = R.mipmap.ic_ineffective;
                    helper.setGone(R.id.image, true);
                } else {
                    helper.setGone(R.id.image, false);
                }
            } else if (TextUtils.equals("1", item.getBonusStatus())) {
                priceColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.secondary_text);
                nameColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.primary_text);
                drawable = R.mipmap.ic_used;
            } else if (TextUtils.equals("2", item.getBonusStatus())) {
                priceColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text);
                nameColor = ContextCompat.getColor(CommonApp.getInstance(), R.color.third_text);
                drawable = R.mipmap.ic_expired;
            }

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(CommonApp.getInstance().getString(net.caixiaomi.info.R.string.C_MONEY_CHAT)).append(item.getBonusPrice());
            int start = stringBuilder.indexOf(item.getBonusPrice());
            SpannableString spannableString = new SpannableString(stringBuilder);
            spannableString.setSpan(new RelativeSizeSpan(1.5f), start, stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            helper.setText(R.id.price, spannableString);
            helper.setTextColor(R.id.price, priceColor);

            stringBuilder.setLength(0);
            stringBuilder.append(item.getMinGoodsAmount()).append("\n").append(item.getUseRange());
            spannableString = new SpannableString(stringBuilder);
            if (!TextUtils.isEmpty(item.getUseRange())) {
                start = stringBuilder.length() - item.getUseRange().length();
                spannableString.setSpan(new RelativeSizeSpan(0.8f), start, stringBuilder.length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            helper.setText(R.id.name, spannableString);
            helper.setTextColor(R.id.name, nameColor);

            stringBuilder.setLength(0);
            if (!TextUtils.isEmpty(item.getLimitTime())) {
                stringBuilder.append(CommonApp.getInstance().getString(net.caixiaomi.info.R.string.C_EXPIRY)).append(": ").append(item.getLimitTime()).append("\n");
            }

            if (!TextUtils.isEmpty(item.getUseRange())) {
                stringBuilder.append(CommonApp.getInstance().getString(net.caixiaomi.info.R.string.C_USE_INFO)).append(": ").append(item.getUseRange()).append("\n");
            }

            if (!TextUtils.isEmpty(item.getLeaveTime())) {
                stringBuilder.append(item.getLeaveTime()).append("\n");
            }

            stringBuilder.deleteCharAt(stringBuilder.length() - 1);

            spannableString = new SpannableString(stringBuilder);
            if (!TextUtils.isEmpty(item.getLimitTime())) {
                start = stringBuilder.indexOf(item.getLimitTime());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.secondary_text)), start, start + item.getLimitTime().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }

            if (!TextUtils.isEmpty(item.getUseRange())) {
                start = stringBuilder.indexOf(item.getUseRange());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.secondary_text)), start, start + item.getUseRange().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            if (!TextUtils.isEmpty(item.getLeaveTime())) {
                start = stringBuilder.indexOf(item.getLeaveTime());
                spannableString.setSpan(new ForegroundColorSpan(ContextCompat.getColor(CommonApp.getInstance(), R.color.secondary_text)), start, start + item.getLeaveTime().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new RelativeSizeSpan(1.1f), start, start + item.getLeaveTime().length(), SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
            }

            helper.setText(R.id.text, spannableString);

            ImageView imageView = helper.getView(R.id.image);
            imageView.setBackgroundResource(drawable);

            if (helper.getAdapterPosition() == 0) {
                RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
                layoutParams.topMargin = (int) CommonApp.getInstance().getResources().getDimension(net.caixiaomi.info.R.dimen.padding_8);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
